package com.lsf.dunlian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lsf.dunlian.R;
import com.lsf.dunlian.activity.GroupChatActivity;
import com.lsf.dunlian.entity.UserData;
import com.makeramen.roundedimageview.RoundedImageView;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.netty.util.internal.StringUtil;

public class SessionRecyclerViewAdapter extends RecyclerView.Adapter<SessionRecyclerViewAdapter.ViewHolder> {
    private List<UserData> mDataList;
    private Context context;

    public SessionRecyclerViewAdapter(List<UserData> mDataList, Context context) {
        this.mDataList = mDataList;
        this.context = context;
    }

    @NonNull
    @Override
    public SessionRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_session, parent, false);
        SessionRecyclerViewAdapter.ViewHolder holder = new SessionRecyclerViewAdapter.ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserData userData = mDataList.get(position);
        holder.name.setText(userData.getName());
        if (StringUtils.isNotEmpty(userData.getMessage())) {
            holder.message.setText(userData.getMessage());
        }
//        if (StringUtils.isNotEmpty(userData.getIcon())) {
//            Glide.with(context).load(userData.getIcon()).into(holder.icon);
//        }

        if (StringUtils.isNotEmpty(userData.getMessageTime())){
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            try {
//                Date parse = sdf.parse(userData.getMessageTime());
//                Calendar calendar = Calendar.getInstance();
//                calendar
//                calendar.setTime(parse);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
            holder.time.setVisibility(View.VISIBLE);
            holder.time.setText(userData.getMessageTime());
        }else {
            holder.time.setVisibility(View.GONE);
        }

        holder.layout.setOnClickListener(v -> {
            Intent intent = new Intent(context, GroupChatActivity.class);
            intent.putExtra("receiveUserId",userData.getUserId());
            intent.putExtra("receiveUserName",userData.getName());
            context.startActivity(intent);
        });


    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.message)
        TextView message;
        @BindView(R.id.layout)
        LinearLayout layout;
        @BindView(R.id.icon)
        RoundedImageView icon;
        @BindView(R.id.time)
        TextView time;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
